﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace rimz
{
    public partial class Default : System.Web.UI.Page
    {
        protected RimzDataDataContext dataContext;

        protected void Page_Load(object sender, EventArgs e)
        {
            dataContext = new RimzDataDataContext();
        }

        protected void UserLogin_Authenticate(object sender, AuthenticateEventArgs e)
        {
            if (SiteLevelAuthenticate(UserLogin.UserName, UserLogin.Password))
            {
                Session["username"] = UserLogin.UserName;
                Response.Redirect("Home.aspx");
            }
        }

        /// <summary>
        /// Return true if username and password are found in the database.
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        private bool SiteLevelAuthenticate(string username, string password)
        {
            bool returnValue = false;
                        
            try
            {
                var userCount = (from u in dataContext.Users
                           where u.UserLogin == username && u.Password == password
                           select u).Count();
                
                if (userCount > 0)
                {
                    returnValue = true;
                }
                
            }
            catch (Exception exp) 
            { 
                exp.ToString(); 
            }
            
            return returnValue;
        }

    } // class
} // namespace
